package com.xiaobai.login;

import com.xiaobai.common.base.BaseResponse;
import com.xiaobai.common.base.CommonRuntimeException;
import com.xiaobai.common.base.ErrorCode;
import com.xiaobai.login.request.LoginUserReq;
import com.xiaobai.login.request.UserPasswordUpdateReq;
import com.xiaobai.login.request.UserPhotoDelReq;
import com.xiaobai.login.request.UserUpdateReq;
import com.xiaobai.login.response.LoginRes;
import com.xiaobai.login.response.UserInfoRes;
import com.xiaobai.login.service.LoginService;
import com.xiaobai.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Base64;

/**
 * 登录Controller
 *
 * @author bail
 * @date 2018/12/29.17:35
 */
@Api(tags = "LoginController-用户登录/登出API")
@RestController()
@RequestMapping("/user")
@Validated
@Slf4j
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 管理员登录
     */
    @ApiOperation(value = "管理员登录", notes = "base64编码即可")
    @PostMapping(value = "/login")
    public BaseResponse<LoginRes> login(@Valid @RequestBody LoginUserReq loginUserReq) {
        String account = new String(Base64.getUrlDecoder().decode(loginUserReq.getAccount().getBytes()));
        String password = new String(Base64.getUrlDecoder().decode(loginUserReq.getPassword().getBytes()));
        loginUserReq.setAccount(account);
        loginUserReq.setPassword(password);
        return BaseResponse.success(loginService.login(loginUserReq));
    }

    /**
     * 获取登录人信息
     */
    @ApiOperation(value = "获取登录人信息", notes = "...")
    @GetMapping(value = "/login/query")
    public BaseResponse<UserInfoRes> loginQuery() {
        return BaseResponse.success(loginService.loginQuery());
    }

    @ApiOperation(value = "删除用户头像", notes = "...")
    @DeleteMapping("/photo/delete")
    public BaseResponse deleteUserPhoto(@RequestBody @Valid UserPhotoDelReq userPhotoDelReq) {
        loginService.deleteUserPhoto(userPhotoDelReq);
        return BaseResponse.SUCCESSFUL();
    }

    /**
     * 修改登录人基本信息
     */
    @ApiOperation(value = "修改登录人基本信息", notes = "...")
    @PutMapping(value = "/update")
    public BaseResponse updateUserInfo(@Valid @RequestBody UserUpdateReq userUpdateReq) {
        if (!userUpdateReq.getUserId().equals(LoginUtil.getUserId())) {
            throw new CommonRuntimeException(ErrorCode.METHOD_NOT_ALLOWED);
        }
        loginService.updateUserInfo(userUpdateReq);
        return BaseResponse.SUCCESSFUL();
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码", notes = "base64编码即可")
    @PutMapping(value = "/password/update")
    public BaseResponse updatePassword(@Valid @RequestBody UserPasswordUpdateReq userPasswordUpdateReq) {
        if (!userPasswordUpdateReq.getUserId().equals(LoginUtil.getUserId())) {
            throw new CommonRuntimeException(ErrorCode.METHOD_NOT_ALLOWED);
        }
        userPasswordUpdateReq.setUserPassword(
                new String(Base64.getUrlDecoder().decode(userPasswordUpdateReq.getUserPassword().getBytes())));
        loginService.updatePassword(userPasswordUpdateReq);
        return BaseResponse.SUCCESSFUL();
    }
}
